/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cpa_cad_questoes_new.java
 *
 * Created on 25/05/2010, 23:26:37
 */

package siga.visao_cpa;

import java.sql.SQLException;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.visao_busca.buscaCpaAvaliacao;
import siga.visao.index;
import siga.visao.verErros;

/**
 *
 * @author rafaelfrasson
 */
public class cpa_cad_questoes_new extends javax.swing.JDialog {
    Object[] JOopcoes = {"Sim","Não"};
    /** Creates new form cpa_cad_questoes_new */
    public cpa_cad_questoes_new(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfDescricao = new javax.swing.JTextField();
        barraFerramentaFonecedor = new javax.swing.JToolBar();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btRemover = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        btSair = new javax.swing.JButton();
        jtfCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfTipo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jtfDesc_avaliacao = new javax.swing.JTextField();
        jtfCod_avaliacao = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrando Questões - CPA");

        jLabel1.setText("Questão:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfDescricao.setEnabled(false);
        jtfDescricao.setName("jtfDescricao"); // NOI18N

        barraFerramentaFonecedor.setFloatable(false);
        barraFerramentaFonecedor.setRollover(true);
        barraFerramentaFonecedor.setName("barraFerramentaFonecedor"); // NOI18N

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/novo_32x32.png"))); // NOI18N
        btNovo.setMnemonic('N');
        btNovo.setText("Novo");
        btNovo.setToolTipText("Inseri um Novo Cadastro");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btNovo.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btNovo.setName("btNovo"); // NOI18N
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btNovo);

        btAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/editar_32x32.png"))); // NOI18N
        btAlterar.setMnemonic('A');
        btAlterar.setText("Alterar");
        btAlterar.setToolTipText("Alterar um  Cadastro");
        btAlterar.setEnabled(false);
        btAlterar.setFocusable(false);
        btAlterar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btAlterar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btAlterar.setName("btAlterar"); // NOI18N
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btAlterar);

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/cancelar_32x32.png"))); // NOI18N
        btCancelar.setMnemonic('C');
        btCancelar.setText("Cancelar");
        btCancelar.setToolTipText("Cancelar um Cadastro");
        btCancelar.setEnabled(false);
        btCancelar.setFocusable(false);
        btCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btCancelar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btCancelar.setName("btCancelar"); // NOI18N
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btCancelar);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/salvar_32x32.png"))); // NOI18N
        btSalvar.setMnemonic('S');
        btSalvar.setText("Salvar");
        btSalvar.setToolTipText("Salvar um Cadastro");
        btSalvar.setEnabled(false);
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSalvar.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSalvar.setName("btSalvar"); // NOI18N
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSalvar);

        btRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/remover_32x32.png"))); // NOI18N
        btRemover.setMnemonic('R');
        btRemover.setText("Remover");
        btRemover.setToolTipText("Remove um Cadastro");
        btRemover.setEnabled(false);
        btRemover.setFocusable(false);
        btRemover.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btRemover.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btRemover.setName("btRemover"); // NOI18N
        btRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoverActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btRemover);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton3.setText("Buscar");
        jButton3.setToolTipText("Busca somente Docentes");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(jButton3);

        btSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sair_32x32.png"))); // NOI18N
        btSair.setMnemonic('S');
        btSair.setText("Sair");
        btSair.setToolTipText("Sair da Janela");
        btSair.setFocusable(false);
        btSair.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btSair.setMargin(new java.awt.Insets(2, 2, 2, 4));
        btSair.setName("btSair"); // NOI18N
        btSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSairActionPerformed(evt);
            }
        });
        barraFerramentaFonecedor.add(btSair);

        jtfCodigo.setEnabled(false);
        jtfCodigo.setName("jtfCodigo"); // NOI18N

        jLabel2.setText("Tipo de Questão:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Objetivo", "Subjetiva" }));
        jtfTipo.setEnabled(false);
        jtfTipo.setName("jtfTipo"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Tipo", "Questão"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setMinWidth(80);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(80);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(80);
        jTable1.getColumnModel().getColumn(1).setMinWidth(80);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(80);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(80);

        jtfDesc_avaliacao.setEnabled(false);
        jtfDesc_avaliacao.setName("jtfDesc_avaliacao"); // NOI18N

        jtfCod_avaliacao.setEnabled(false);
        jtfCod_avaliacao.setName("jtfCod_avaliacao"); // NOI18N

        jLabel3.setText("Avaliação:");
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 645, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE))
                    .addComponent(jLabel2)
                    .addComponent(jtfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jtfCod_avaliacao, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfDesc_avaliacao, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(barraFerramentaFonecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCod_avaliacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesc_avaliacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        if(!jtfCod_avaliacao.getText().equals("")){
            btSalvar.setEnabled(!btSalvar.isEnabled());
            btCancelar.setEnabled(!btCancelar.isEnabled());
            btNovo.setEnabled(!btNovo.isEnabled());
            btAlterar.setEnabled(false);
            btRemover.setEnabled(false);
            gerenciaCampos();
            LimpaCampos();
        }else{
            JOptionPane.showMessageDialog(null, "Favor buscar uma Avaliação.","Alerta do Sistema",JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_btNovoActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        gerenciaBotao();
        gerenciaCampos();
}//GEN-LAST:event_btAlterarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        if(btCancelar.isEnabled()){
            if(!btNovo.isEnabled()){
                btSalvar.setEnabled(!btSalvar.isEnabled());
                btCancelar.setEnabled(!btCancelar.isEnabled());
                btNovo.setEnabled(!btNovo.isEnabled());
            }else{
                gerenciaBotao();
            }
            gerenciaCampos();
            LimpaCampos();
        }
}//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if(!jtfDescricao.getText().equals("")){
            String cp="",vl="";
            if(!jtfCodigo.getText().equals("")){
                cp = "codigo,";
                vl = ""+jtfCodigo.getText()+",";
            }
            conexao.Inserir("replace into cpa_questoes ("+cp+" cod_emp, cod_avaliacao,descricao,tipo) " +
                    "values("+vl+index.cod_emp+","+jtfCod_avaliacao.getText()+", \""+jtfDescricao.getText()+"\""+", \""+jtfTipo.getSelectedItem().toString()+"\")");
            gerenciaBotao();
            gerenciaCampos();
            LimpaCampos();
            listaGrupo();
        }else{
            JOptionPane.showMessageDialog(null, "Todos os campos devem estar preenchidos.","Alerta do Sistema.",JOptionPane.INFORMATION_MESSAGE);
        }
}//GEN-LAST:event_btSalvarActionPerformed

    private void btRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoverActionPerformed
        if (JOptionPane.showOptionDialog(null, "Deseja remover este Questao?\nEsta ação implica em remover todos os registros das Questões aplicadas até o momento.", "Removendo Curso", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, JOopcoes, JOopcoes[0]) == JOptionPane.YES_OPTION) {
            try {
                conexao.stm.executeUpdate("delete from cpa_questoes where codigo="+jtfCodigo.getText()+" and cod_emp="+index.cod_emp+" and cod_avaliacao="+jtfCod_avaliacao.getText());
                LimpaCampos();
                listaGrupo();
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
        }
}//GEN-LAST:event_btRemoverActionPerformed

    @SuppressWarnings("static-access")
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        buscaCpaAvaliacao bca = new buscaCpaAvaliacao(null,true);
        bca.setLocation((index.tela.width-bca.getWidth())/2, (index.tela.height-bca.getHeight())/2);
        bca.setVisible(true);
        if(bca.bcpaa_finaliza){
            jtfCod_avaliacao.setText(bca.bcpaa_dados.get(0).toString());
            jtfDesc_avaliacao.setText(bca.bcpaa_dados.get(1).toString());
            listaGrupo();
        }
}//GEN-LAST:event_jButton3ActionPerformed

    private void btSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSairActionPerformed
        btCancelarActionPerformed(evt);
        setVisible(false);
        dispose();
}//GEN-LAST:event_btSairActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount() == 2){
            if (btNovo.isEnabled()) {
                btAlterar.setEnabled(true);
                btRemover.setEnabled(true);
            }
            javax.swing.table.DefaultTableModel jtm = (javax.swing.table.DefaultTableModel)jTable1.getModel();
            jtfCodigo.setText(jtm.getValueAt(jTable1.getSelectedRow(), 0).toString());
            jtfTipo.setSelectedItem(jtm.getValueAt(jTable1.getSelectedRow(), 1));
            jtfDescricao.setText(jtm.getValueAt(jTable1.getSelectedRow(), 2).toString());
        }
    }//GEN-LAST:event_jTable1MouseClicked


    private void gerenciaCampos(){
        jtfTipo.setEnabled(!jtfDescricao.isEnabled());
        jtfDescricao.setEnabled(!jtfDescricao.isEnabled());
    }

    private void LimpaCampos(){
        jtfCodigo.setText("");
        jtfDescricao.setText("");
    }

    private void gerenciaBotao(){
        btNovo.setEnabled(!btNovo.isEnabled());
        btAlterar.setEnabled(!btAlterar.isEnabled());
        btCancelar.setEnabled(!btCancelar.isEnabled());
        btRemover.setEnabled(!btRemover.isEnabled());
        btSalvar.setEnabled(!btSalvar.isEnabled());
    }

    private void listaGrupo(){
        int i = jTable1.getRowCount();
        javax.swing.table.DefaultTableModel jtm = (javax.swing.table.DefaultTableModel)jTable1.getModel();
        for(int x=(i-1);x>=0; --x ){
            jtm.removeRow(x);
        }
        try {
            conexao.Query("select * from cpa_questoes where cod_avaliacao="+jtfCod_avaliacao.getText()+" and cod_emp="+index.cod_emp+" order by codigo");
            while (conexao.rs.next()) {
                jtm.addRow(new Object[]{conexao.rs.getString("codigo"),conexao.rs.getString("tipo"),conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToolBar barraFerramentaFonecedor;
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btRemover;
    private javax.swing.JButton btSair;
    private javax.swing.JButton btSalvar;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jtfCod_avaliacao;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfDesc_avaliacao;
    private javax.swing.JTextField jtfDescricao;
    private javax.swing.JComboBox jtfTipo;
    // End of variables declaration//GEN-END:variables

}
